package com.zjy.software.day09;

public class StringBufferDemo {
    public static void main(String[] args)
    {
//        testStringIterAndStringBuffer();

        StringBuffer stringBuffer = new StringBuffer("abcde");
        stringBuffer.insert(1,"123");
        System.out.println("stringBuffer = " + stringBuffer);
        stringBuffer.delete(1,3);
        System.out.println("stringBuffer = " + stringBuffer);

        //正则表达式的测试
        String number="18912123456";
        boolean matches = number.matches("1[345789][0-9]{9}");
        if (matches ) {
            System.out.println("手机号格式正确");
        }else {
            System.out.println("手机号格式错误");
        }
    }

    private static void testStringIterAndStringBuffer() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("hello");
        stringBuffer.append(123);
        stringBuffer.append(true);

        System.out.println("stringBuffer = " + stringBuffer);

        //如果有初始化字符串,更多的操作是把字符串传入到StringBuffer的构造器里面
        StringBuffer tempStringBuffer=new StringBuffer("我是字符串的构造器参数");
        System.out.println("tempStringBuffer = " + tempStringBuffer);

        long start = System.currentTimeMillis();
        String testStr="hello";
        for (int i = 0; i < 100000; i++) {
            testStr+=i;
        }
        long end = System.currentTimeMillis();
//        System.out.println(testStr);
        System.out.println("String 所用的时间：" + (end - start));

        start = System.currentTimeMillis();
        testStr="hello";
        StringBuffer paramStringBuffer=new StringBuffer(testStr);
        for (int i = 0; i < 100000; i++) {
            paramStringBuffer.append(i);

        }
        end = System.currentTimeMillis();
//        System.out.println(paramStringBuffer);
        System.out.println("StringBuffer 所用的时间：" + (end - start));


        start = System.currentTimeMillis();
        testStr="hello";
        StringBuilder paramStringBuilder=new StringBuilder(testStr);
        for (int i = 0; i < 100000; i++) {
            paramStringBuilder.append(i);

        }
        end = System.currentTimeMillis();
//        System.out.println(paramStringBuffer);
        System.out.println("StringBuilder 所用的时间：" + (end - start));



    }
}
